/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;

import java.util.List;
import model.pojo.Deal;
import model.pojo.Khachhang;
import model.util.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author CUCAINHO
 */
public class DealDAO {
    
    
    public static List<Deal> LayDanhSachDeal() { 
            
    List<Deal> danhSachDeal = null;
    
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
            String hql = "select deal from Deal deal"; 
            Query query = session.createQuery(hql);
            //Query query = session.createQuery(hql);             
            danhSachDeal = query.list(); 
        }catch (HibernateException ex) 
        { //Log the exception 
            System.err.println(ex); 
        } 
        finally {
            session.close(); 
        } 
    return danhSachDeal; 
    }
    
    public static List<Deal> LayDanhSachDeal(int maDanhMuc) { 
            
    List<Deal> danhSachDeal = null;
    
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
           String hql = " from Deal deal where deal.maDanhMuc=:maDanhMuc";

            Query query = session.createQuery(hql);
            query.setString("maDanhMuc", Integer.toString(maDanhMuc));
            

            //Query query = session.createQuery(hql);
            //Query query = session.createQuery(hql);             
           danhSachDeal = query.list();
            
        }catch (HibernateException ex) 
        { //Log the exception 
            System.err.println(ex); 
        } 
        finally {
            session.close(); 
        } 
    return danhSachDeal; 
    }
    
    public static List<Deal> LayDanhSachDeal(int maDanhMuc , int maDiaDiem) { 
            
    List<Deal> danhSachDeal = null;
    
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
            String hql = "select deal from Deal deal"; 
            Query query = session.createQuery(hql);
            //Query query = session.createQuery(hql);             
            danhSachDeal = query.list(); 
        }catch (HibernateException ex) 
        { //Log the exception 
            System.err.println(ex); 
        } 
        finally {
            session.close(); 
        } 
    return danhSachDeal; 
    }
    
    
    public static List<Deal> LayDanhSachDealTheoDiaDiem(int maDiaDiem) { 
            
    List<Deal> danhSachDeal = null;
    
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
            String hql = "select deal from Deal deal"; 
            Query query = session.createQuery(hql);
            //Query query = session.createQuery(hql);             
            danhSachDeal = query.list(); 
        }catch (HibernateException ex) 
        { //Log the exception 
            System.err.println(ex); 
        } 
        finally {
            session.close(); 
        } 
    return danhSachDeal; 
    }
    
     public static Deal LayDeal(int maDeal) { 
            
   Deal deal = null;
    
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
            String hql = "from Deal deal where deal.maDeal=:maDeal"; 
            
             Query query = session.createQuery(hql);
            query.setString("maDeal", Integer.toString(maDeal));
                  
            deal = (Deal) query.list().get(0); 
        }catch (HibernateException ex) 
        { //Log the exception 
            System.err.println(ex); 
        } 
        finally {
            session.close(); 
        } 
    return deal; 
    }
     
     
    
    
    
}
